package com.phf.controller;

import com.phf.common.Result;
import com.phf.common.core.annotation.ResponseEncrypt;
import com.phf.component.ExampleComponent;
import com.phf.component.ResultComponent;
import com.phf.config.CommonWebConfig;
import com.phf.domain.TbStudentEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.phf.component.ExampleComponent.apiUrl;


/**
 * @ClassName : TbStudentController
 * @Description : 前端控制器Controller测试类
 * @Author : AD
 */

@RestController
@RequestMapping("/demo")
@RefreshScope
@Slf4j
public class TbStudentController{

    @Resource
    private TbStudentEntity userConfig;

    @Value("${config.info}")
    private String configInfo;

    @Autowired
    private CommonWebConfig commonWebConfig;

    @PostMapping("/encryptPost")
    @ResponseEncrypt
    public Result<TbStudentEntity> postDemo(@RequestBody TbStudentEntity tbStudentEntity, HttpServletRequest request){
        System.out.println("request.getParameter(\"aesKey\") = " + request.getParameter("aesKey"));
        //System.out.println("request.getAttribute(\"aesKey\") = " + request.getAttribute("aesKey"));
        System.out.println("tbStudentEntity = " + tbStudentEntity);
        return Result.ok(tbStudentEntity);
    }

    @GetMapping("/encryptGet")
    @ResponseEncrypt
    public Result<?> postDemoGet(HttpServletRequest request,@RequestParam String id,@RequestParam String name,@RequestParam String aesKey ){
        System.out.println("id = " + id);
        System.out.println("name = " + name);
        System.out.println("aesKey = " + aesKey);
        System.out.println("request.getParameter(\"age\") = " + request.getParameter("age"));
        //System.out.println("request.getAttribute(DecryptReplaceStreamFilter.AKS_PARAMETER) = " + request.getAttribute(DecryptReplaceStreamFilter.AKS_PARAMETER));
        return Result.ok();
    }


    @GetMapping("/user/hello")
    public String hello() {
        return userConfig.getId() + userConfig.getName() + userConfig.getAge() + userConfig.getSex();
    }

    @GetMapping("/config/info")
    public String getConfigInfo() {
        return configInfo;
    }

    @GetMapping("/config/apiUrl")
    public Result<?> getApiUrl() {
        return Result.ok("success",apiUrl);
    }

    @GetMapping("/config/apiComponent")
    public Result<?> getApiComponent() {
        return ResultComponent.result;
    }
}
